package com.example.work.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.work.entity.DbUser;
import com.example.work.entity.TbCheckBatch;
import com.example.work.entity.TbHospCheckCategory;
import com.example.work.entity.vo.BatchAddVo;
import com.example.work.service.ITbCheckBatchService;
import com.example.work.service.ITbHospCheckCategoryService;
import com.example.work.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/batch")
public class BatchController {


    @Autowired
    private ITbCheckBatchService batchService;

    @Autowired
    private ITbHospCheckCategoryService hospCheckCategoryService;

    /**
     * 返回批次列表，分页
     * **/
    @GetMapping("/page")
    public R page(@RequestParam(value = "current", defaultValue = "1") int page,
                  @RequestParam(value = "size", defaultValue = "10") int size){

        try {
            // 分页查询区域列表
            IPage<TbCheckBatch> areaPage = batchService.page(new Page<>(page, size));

            // 组装分页结果
            Map<String, Object> map = new HashMap<>();
            map.put("records", areaPage.getRecords()); // 当前页数据
            map.put("total", areaPage.getTotal()); // 总记录数
            map.put("pages", areaPage.getPages()); // 总页数
            map.put("current", areaPage.getCurrent()); // 当前页码
            map.put("size", areaPage.getSize()); // 每页大小

            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("获取列表失败：" + e.getMessage());
        }
    }

    // 返回批次列表
    @GetMapping("/list")
    public R list(){
        String username = "Soybean";

        return null;
    }

    /**
     * 根据批次的id返回该批次的详细信息
     * **/
    @GetMapping("/info/{id}")
    public R getBatchById(@PathVariable String id){
        TbCheckBatch batch = batchService.getById(id);
        if(null == batch)
            return R.error("无效批次id");
        Map<String, Object> map = new HashMap<>();
        map.put("data",batch);
        return R.ok(map);
    }

    /**
     * 新增批次
     * **/
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public R addBatch(@RequestBody BatchAddVo batchAddVo){

        try{
            // 获取批次信息
            TbCheckBatch batch = batchAddVo;
            boolean flag = batchService.save(batch);
            if(!flag)
                throw new Exception();


            for(int i = 0; i < batchAddVo.getCategoryIds().length; i++){
                TbHospCheckCategory hospCheckCategory = new TbHospCheckCategory();

                hospCheckCategory.setBatchId(batch.getId());
                hospCheckCategory.setHospId(batchAddVo.getHospId());
                hospCheckCategory.setCategoryId(batchAddVo.getCategoryIds()[i]);
                if(!hospCheckCategoryService.save(hospCheckCategory))
                    throw new Exception();
            }
            return R.ok("新增批次成功");

        }catch (Exception e){
            e.printStackTrace();
            return R.error("新增批次失败" + e.getMessage());
        }
    }
    @GetMapping("/all")
    public R getAllHosp() {
        Map<String,Object> map=new HashMap<>();
        map.put("batchList",batchService.list());
        return R.ok(map);
    }

}
